import {createRouter, createWebHistory, RouteRecordRaw} from 'vue-router'

const routes: Array<RouteRecordRaw> = [
    {path: '/', redirect: '/login'},
    {path: '/login', name:'登录', component: ()=>import('@/components/login/Login.vue')},
    {
        path: '/home',
        name:'首页',
        component: ()=>import('@/components/home/Home.vue'),
        redirect: '/welcome',
        children: [
            {path: '/welcome', name: '欢迎', component: ()=>import('@/components/welcome/Welcome.vue')},
            {path: '/users', name: '用户列表', component: ()=>import('@/components/users/Users.vue')},

            {path: '/rights', name: '权限列表', component: ()=>import('@/components/rights/Rights.vue')},
            {path: '/roles', name: '角色列表', component: ()=>import('@/components/rights/Roles.vue')},

            {path: '/goods', name: '商品列表', component: ()=>import('@/components/goods/Goods.vue')},
            {path: '/goods/add', name: '添加商品', component: ()=>import('@/components/goods/Add.vue')},
            {path: '/params', name: '分类参数', component: ()=>import('@/components/goods/Params.vue')},
            {path: '/categories', name: '商品分类', component: ()=>import('@/components/goods/Categories.vue')},

            {path: '/orders', name: '订单列表', component: ()=>import('@/components/orders/Orders.vue')},
            {path: '/reports', name: '数据报表', component: ()=>import('@/components/reports/Reports.vue')},
        ]
    },
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

router.beforeEach(((to, from, next) => {
    if (to.path === '/login') return next()
    const tokenStr = window.sessionStorage.getItem('token')
    if (!tokenStr) return next('/login')
    next()
}))

export default router
